<template>
	<view>
		<web-view :src="url" v-if="url" @message="onMessage"></web-view>
	</view>
</template>

<script>
import {
	createSendMessage,
	createC2CConversation
} from "@/common/public";

export default {
	props: {
		propsUrl: {
			type: String,
			default: ''
		},
		identity_type: {
			default: ''
		}
	},
	data() {
		return {
			optionsUrl: "",
			token: '',
			user_id: '',
			url: '',
			lang: 'zh',
			mobile: '',
			statusBarHeight: 0,
			statusbottom: 0,
			params: "",
			nameType: false,
		}
	},
	onLoad(options) {
		if (options.url) {
			console.log("=======================================")
			console.log(options.url)
			const urlWithParams = options.url.split("?")
			if (urlWithParams[1]) {
				this.optionsUrl = urlWithParams[0];
				this.params = urlWithParams[1];
			} else {
				this.optionsUrl = options.url;
			}
		}
		if (options.title) {
			uni.setNavigationBarTitle({
				title: options.title
			})
		} else {
			uni.setNavigationBarTitle({
				title: this.$t('我要发货')
			})
		}
	},
	mounted() {
		this.init();
	},

	methods: {
		messageHandle(e) {

			if (e.data.action) {
				this.onMessage({ detail: { data: [e.data] } })
			}
		},
		// 动态设置webview参数
		isSetParams(data) {
			let currentWebview = this.$scope.$getAppWebview();

			try {
				let wv = currentWebview.children()[0];
				currentWebview.setStyle({
					'popGesture': 'none',
				})
				wv.addEventListener('loaded', function () {
					wv.evalJS(`setParams(${JSON.stringify(data)})`);
				})

			} catch (e) {
				//TODO handle the exception
				console.log("e=============", e)
			}

		},
		init() {
			const isDev = true;
			const devUrl = "http://192.168.1.30:3333";
			this.token = uni.getStorageSync('xzdToken')
			this.user_id = uni.getStorageSync('user_id')
			this.lang = uni.getStorageSync('lang')
			this.mobile = uni.getStorageSync('mobile');
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			this.statusbottom = uni.getSystemInfoSync().safeAreaInsets.bottom;
			var version = getApp().globalData.version;

			this.url = (isDev ? devUrl : 'http://shop.gluz.com.cn/constructor') + (this.optionsUrl || this.propsUrl ||  "/") +
				'?token=' + this.token +
				'&userId=' + this.user_id +
				'&lang=' + this.lang +
				'&mobile=' + this.mobile +
				'&statusBarHeight=' + this.statusBarHeight +
				'&statusbottom=' + this.statusbottom +
				'&identity_type=' + this.identity_type +
				'&version=' + version +
				'&app=1&' + this.params


		},
		onMessage(event) {
			let {
				action,
				data,
				title,
				...otherParams
			} = event.detail.data[0]
			let curentWebView = otherParams.webview || 'zWebViewDaixiao';

			switch (action) {
				case 'navigateBack':
					uni.navigateBack({
						delta: data,
					})
					break;
				case 'navigateTo':
					uni.navigateTo({
						url: `/pages/${curentWebView}/${curentWebView}?title=` + title + "&url=" + encodeURIComponent(data)
					})
					break;
				case 'redirectTo':
					uni.redirectTo({
						url: `/pages/${curentWebView}/${curentWebView}?title=` + title + "&url=" + encodeURIComponent(data)
					})
					break;
				case 'login':
					if (!!this.nameType) return;
					this.nameType = true;
					uni.navigateTo({ 
						url: "/pages/user/login/login"
					})
					this.nameType = false;
					break;

				case 'call':
					createC2CConversation(otherParams.tel, this)
					break;

				case 'meeting':
					let tel = otherParams.tel;
					let username = otherParams.username;
					// 自定义卡片内容
					let text = `${username}向你申请进行视频会议请前往多语管家创建会议并邀请“${username}请及时与“${username}”进行沟通视频会议时 间等相关事项，无限商机，尽在GULZ`
					createSendMessage(tel, text, this)
					break;
					// 设置横屏竖屏
				case 'setLockOrientation':
					// #ifdef APP-PLUS
						plus.screen.lockOrientation(otherParams.params);
					// #endif
					break;


			}




		}
	}
}
</script>